package com.hyg.module_report.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hyg.lib_base.mainUtils.Utils;
import com.hyg.module_report.R;
import com.hyg.module_report.model.ReportListData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


/*   报告列表选择的适配器   */
public class ReportListAdapter extends RecyclerView.Adapter<ReportListAdapter.ViewHolder> {
    private Context context;
    private ArrayList<ReportListData> dataList;
    private OnItemClickLitener mOnItemClickLitener;

    public ReportListAdapter(Context activity, ArrayList<ReportListData> dataList) {
        this.context = activity.getApplicationContext();
        this.dataList = dataList;
    }

    public void addItem(ReportListData content, int position) {
        dataList.add(position, content);
        notifyItemInserted(position); //Attention!
    }

    public void removeItem(ReportListData model) {
        int position = dataList.indexOf(model);
        dataList.remove(position);
        notifyItemRemoved(position);//Attention!
    }


    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_report_list,
                parent, false);
        ViewHolder holder = new ViewHolder(view);

        //        holder.canteenName = (TextView) view.findViewById(R.id.canteenName);
        //        holder.canteen = (ImageView) view.findViewById(R.id.canteen);

        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.bindView(dataList.get(position), position);
    }

    public void removeData(int position) {
        dataList.remove(position);
        notifyItemRemoved(position);
    }

    //    public long getItemId(int position) {
    //        return position;
    //    }

    @Override
    public int getItemCount() {
        if(dataList==null) {
            return 0;
        }
        return dataList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        //        private ReportListData item;
        private TextView report_parient_name;
        private TextView report_date;
        private TextView report_hospital;

        public Date stringToDate(String dateString) {
            //        ParsePosition position = new ParsePosition(0);
            if(Utils.isEmpty(dateString)){
                Date date = new Date();
                return date;
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //        Date dateValue = simpleDateFormat.parse(dateString, position);
            Date dateValue = null;
            try {
                dateValue = simpleDateFormat.parse(dateString);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return dateValue;
        }

        public String dateToString(Date data, String formatType) {
            return new SimpleDateFormat(formatType).format(data);
        }

        public ViewHolder(View itemView) {
            super(itemView);
            report_parient_name = (TextView) itemView.findViewById(R.id.report_parient_name);
            report_date = (TextView) itemView.findViewById(R.id.report_date);
            report_hospital = (TextView) itemView.findViewById(R.id.report_hospital);
        }

        public void bindView(final ReportListData data, final int pos){

            report_parient_name.setText(data.getReport_parient_name());
            Date temp =  stringToDate(data.getReport_date().trim());
            report_date.setText(dateToString(temp,"yyyy年MM月dd日"));
            report_hospital.setText("机构："+data.getReport_hospital());
            report_hospital.setSelected(true);
            // 如果设置了回调，则设置点击事件
            if (mOnItemClickLitener != null)
            {
                itemView.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        int pos = getLayoutPosition();
                        mOnItemClickLitener.onItemClick(itemView, pos);
                        //                    Toast.makeText(context,""+pos,Toast.LENGTH_SHORT).show();
                    }
                });

                itemView.setOnLongClickListener(new View.OnLongClickListener()
                {
                    @Override
                    public boolean onLongClick(View v)
                    {
                        int pos = getLayoutPosition();
                        mOnItemClickLitener.onItemLongClick(itemView, pos);
                        return false;
                    }
                });
            }

        }

    }
}